package com.zc.model.${packageName!};

import com.zc.common.DMLoader;
import com.zc.common.DateUtil;
import com.zc.common.MisConstants;
import com.zc.common.MisException;
import com.zc.common.Operator;
import com.zc.common.PageInfo;
import com.zc.common.StringUtil;
import com.zc.common.upload.ZcFile;
import com.zc.common.upload.ZcFiles;
<#if (zllb?? && zllb != '' && zllb == 'dazl')>
import com.zc.domain.single.GG_DAZL;
</#if>
import com.zc.domain.single.${tableName!};
<#if (lcmc?? && lcmc != '')>
import com.zc.domain.single.YW_DQHJ;
import com.zc.enobj.Ywspxx;
import com.zc.enobj.Ywtsxx;
</#if>
<#if (zllb?? && zllb != '')>
import com.zc.model.XmPublicFacade;
    <#else >
import com.zc.model.PublicFacade;
</#if>
import com.zc.model.entity.XtglBean;
<#if (lcmc?? && lcmc != '')>
import com.zc.model.yw.YwlcService;
</#if>
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
<#if (lcmc?? && lcmc != '')>
    import org.springframework.beans.factory.annotation.Autowired;
</#if>
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

<#if (zllb?? && zllb != '')>
import java.util.ArrayList;
import java.util.HashMap;
</#if>
import java.util.List;
import java.util.Map;

/**
 * ${comments!}
 *
 * @author 董晓峰
 * @datetime ${dqsj!}
 */
@Service(value = "${packageName!}.${className!}Facade")
public class ${className?cap_first!}Facade extends <#if (zllb?? && zllb != '')>XmPublicFacade<#else >PublicFacade</#if> {
    private static final Log log = LogFactory.getLog(${className?cap_first!}Facade.class);
<#if (lcmc?? && lcmc != '')>

    @Autowired
    private YwlcService ywlcService;
</#if>
    /**
     * 查询${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @Transactional(rollbackFor = Exception.class)
    public ${tableName!} get${className?cap_first!}ByKey(String ${className!}id) throws Exception {
        if (StringUtil.isEmpty(${className!}id)) {
            throw new MisException("查询${comments!}，${comments!}编号不能为空，请确认!");
        }
        ${tableName!} ${className!} = new ${tableName!}();
        ${className!}.setId(${className!}id);

        return this.baseDao.query(${className!});
    }

    /**
     * ${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @Transactional(rollbackFor = Exception.class)
    public List list${className?cap_first!}(Map paramMap, PageInfo pageInfo, Operator operator) throws Exception {
        String strSqlKey = "${packageName!}.${className!}Facade.list${className?cap_first!}";
        paramMap.put("czyid", operator.getId());

        return this.baseDao.list(strSqlKey, paramMap, pageInfo);
    }

    /**
     * 保存${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @Transactional(rollbackFor = Exception.class)
    public void save${className?cap_first!}(String ${className!}id, ${tableName!} ${className!}, Operator operator) throws Exception {
        if (StringUtil.isEmpty(${className!}id)) {
            throw new MisException("保存${comments!}，${comments!}编号不能为空，请确认!");
        }
        if (${className!} == null) {
            throw new MisException("保存${comments!}，${comments!}信息不存在，请确认!");
        }
        if (${className!}.isEmptyId()) {
            ${className!}.setId(${className!}id);

            ${className!}.setShzt(MisConstants.BSDY_SHZT_CGX);
            ${className!}.setCjr(operator.getId());
            ${className!}.setCjsj(DateUtil.getTime14());
            this.baseDao.add(${className!});
        } else {
            this.baseDao.mod(${className!});
        }
    }

    /**
     * 删除${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @Transactional(rollbackFor = Exception.class)
    public void del${className?cap_first!}(String ${className!}id) throws Exception {
        if (StringUtil.isEmpty(${className!}id)) {
            throw new MisException("删除${comments!}，${comments!}编号不能为空，请确认!");
        }

        ${tableName!} ${className!} = new ${tableName!}();
        ${className!}.setId(${className!}id);
        this.baseDao.del(${className!});
    }

    <#if (lcmc?? && lcmc != '')>
    /**
     * 提交${comments!}
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @Transactional(rollbackFor = Exception.class)
    public void submit${className?cap_first!}(String ${className!}id, Operator operator) throws Exception {
        if (StringUtil.isEmpty(${className!}id)) {
            throw new MisException("提交${comments!}，${comments!}信息不存在，请确认！");
        }

        ${tableName!} ${className!} = this.get${className?cap_first!}ByKey(${className!}id);
        if (${className!}.getShzt() == MisConstants.BSDY_SHZT_SHTH) {
            YW_DQHJ dqhj = this.ywlcService.getDqhj(MisConstants.LC_${lcmc!}, ${className!}id, operator);
            if (dqhj != null) {
                ${className!}.setShzt(MisConstants.BSDY_SHZT_SHZ);
                this.baseDao.mod(${className!});
                this.ywlcService.forward(dqhj.getId(), operator);
            }
        } else {
            ${className!}.setShzt(MisConstants.BSDY_SHZT_SHZ);
            this.baseDao.mod(${className!});
            this.ywlcService.createYwlcAndNext(MisConstants.LC_${lcmc!}, ${className!}id, "需要自定义" + "-" + DMLoader.getMCByKey("BZLC", MisConstants.LC_${lcmc!}), "", operator);
        }
    }

    /**
     * ${comments!}流程转发
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @Transactional(rollbackFor = Exception.class)
    public Ywtsxx forward(String ${className!}id, Ywspxx spxx, Operator operator) throws Exception {
        if (StringUtil.isEmpty(${className!}id)) {
            throw new MisException("${comments!}流程转发，${comments!}信息不存在，请确认！");
        }
        ${tableName!} ${className!} = this.get${className?cap_first!}ByKey(${className!}id);

        if (${className!}.getShzt() == MisConstants.BSDY_SHZT_SHTH || ${className!}.getShzt() == MisConstants.BSDY_SHZT_CGX) {
            ${className!}.setShzt(MisConstants.BSDY_SHZT_SHZ);
            this.baseDao.mod(${className!});
        }

        return this.ywlcService.forward(spxx, operator);
    }
    <#if (isYQxsq)>

    /**
     * 取消申请
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @Transactional(rollbackFor = Exception.class)
    public void qx${className?cap_first!}(String ${className!}id, Operator operator) throws Exception {

        if (StringUtil.isEmpty(${className!}id)) {
            throw new MisException("取消${comments!}，${comments!}编号不能为空，请确认！");
        }
        ${tableName!} ${className!} = this.get${className?cap_first!}ByKey(${className!}id);
        ${className!}.setShzt(MisConstants.BSDY_SHZT_QXSC);
        this.baseDao.mod(${className!});

        this.ywlcService.stopLcsl(MisConstants.LC_${lcmc!}, ${className!}id, operator);
    }
    </#if>
    </#if>
    <#if (zllb?? && zllb != '' && zllb == 'dazl')>

    /**
     * 查询附件资料
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @Transactional(rollbackFor = Exception.class)
    public Map m${className?cap_first!}zl(String ${className!}id) throws Exception {
        if (StringUtil.isEmpty(${className!}id)) {
            throw new MisException("查询附件资料，${comments!}编号不能为空，请确认！");
        }
        Map mDazl = new HashMap();
        List<GG_DAZL> listDazl = this.listDazl(${className!}id, null, null, null);

        for (int i = 0; listDazl != null && i < listDazl.size(); i++) {
            GG_DAZL dazl = listDazl.get(i);

            String zllb = dazl.getZllb();
            List listZl;
            if (mDazl.containsKey(zllb)) {
                listZl = (List) mDazl.get(zllb);
            } else {
                listZl = new ArrayList();
            }
            listZl.add(dazl);
            mDazl.put(zllb, listZl);
        }

        return mDazl;
    }

    /**
     * 上传附件
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @Transactional(rollbackFor = Exception.class)
    public void upload${className?cap_first!}fj(String zllb, String ${className!}id, String czyid, ZcFiles files) throws Exception {
        if (StringUtil.isEmpty(zllb)) {
            throw new MisException("上传附件，资料类别不能为空，请确认！");
        }
        if (StringUtil.isEmpty(${className!}id)) {
            throw new MisException("上传附件，${comments!}编号不能为空，请确认！");
        }

        if (files.getCount() == 0) {
            return;
        }
        ZcFile file = files.getFile(0);

        XtglBean bean = new XtglBean(baseDao);

        bean.uploadDazl(StringUtil.getUUID(), zllb, ${className!}id,  file, czyid);
    }

    /**
     * 删除附件
     *
     * @author 董晓峰
     * @datetime ${dqsj!}
     */
    @Transactional(rollbackFor = Exception.class)
    public void del${className?cap_first!}fj(String zlid) throws Exception {
        if (StringUtil.isEmpty(zlid)) {
            throw new MisException("删除附件资料，附件编号不允许为空，请确认！");
        }
        XtglBean bean = new XtglBean(baseDao);
        bean.delDazlFile(zlid);
    }
    </#if>
}